package com.snail.billing.page;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpConst;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.ResUtil;
import com.snail.billing.Resource;
import com.snail.billing.data.Account;
import com.snail.billing.data.AccountManager;
import com.snail.billing.data.WorkDataManager;
import com.snail.billing.net.BillingHttpApp;
import com.snail.billing.os.DialogPage;
import com.snail.billing.page.view.UserCenterAdapter;
import com.snail.billing.session.GeneralLoginSession;
import com.snail.billing.session.ValidateInfoSession;

public class UserCenterPage extends DialogPage implements View.OnClickListener, AdapterView.OnItemClickListener, OnHttpResultListener {

	/** 绑定账号 */
	public static final int BIND_ACCOUNT = 1;
	/** 绑定手机 */
	public static final int BIND_MOBILE = 2;
	/** 绑定邮箱 */
	public static final int BIND_EMAIL = 3;
	/** 修改密码 */
	public static final int MODIFY_PASSWORD = 4;
	/** 在线提问 */
	public static final int ONLINE_QUESTION = 5;
	/** 客服 */
	public static final int CUSTOM_SERVICE = 6;

	private TextView textTitle;
	private View buttonBack;

	private ListView listView;
	private UserCenterAdapter adapter;
	private List<UserCenterAdapter.AdapterData> data;

	private List<Integer> userCenterTypes;

	private ValidateInfoSession validateInfoSession;
	private ValidateInfoSession.JsonData jsonValidateInfo;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_user_center_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_user_center_title));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		listView = (ListView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_user_center_list_view));
		listView.setOnItemClickListener(this);
		//
		userCenterTypes = new ArrayList<Integer>();
		if (AccountManager.getCurrentAccount().getType().equals(Account.TYPE_RANDOM) || AccountManager.getCurrentAccount().getType().equals(Account.TYPE_RANDOM_OLD)) {
			userCenterTypes.add(BIND_ACCOUNT);
			userCenterTypes.add(ONLINE_QUESTION);
			userCenterTypes.add(CUSTOM_SERVICE);
		} else {
			userCenterTypes.add(BIND_MOBILE);
			userCenterTypes.add(BIND_EMAIL);
			userCenterTypes.add(MODIFY_PASSWORD);
			userCenterTypes.add(ONLINE_QUESTION);
			userCenterTypes.add(CUSTOM_SERVICE);
		}
		//
		data = new ArrayList<UserCenterAdapter.AdapterData>();
		for (int i = 0; i < userCenterTypes.size(); i++) {
			if (userCenterTypes.get(i) == BIND_ACCOUNT) {
				int imageId = ResUtil.getDrawableId(Resource.drawable.snailbilling_uc_account);
				int stringId = ResUtil.getStringId(Resource.string.snailbilling_user_center_text_account);
				data.add(new UserCenterAdapter.AdapterData(imageId, stringId));
			} else if (userCenterTypes.get(i) == BIND_MOBILE) {
				int imageId = ResUtil.getDrawableId(Resource.drawable.snailbilling_uc_mobile);
				int stringId = ResUtil.getStringId(Resource.string.snailbilling_user_center_text_mobile);
				data.add(new UserCenterAdapter.AdapterData(imageId, stringId));
			} else if (userCenterTypes.get(i) == BIND_EMAIL) {
				int imageId = ResUtil.getDrawableId(Resource.drawable.snailbilling_uc_email);
				int stringId = ResUtil.getStringId(Resource.string.snailbilling_user_center_text_email);
				data.add(new UserCenterAdapter.AdapterData(imageId, stringId));
			} else if (userCenterTypes.get(i) == MODIFY_PASSWORD) {
				int imageId = ResUtil.getDrawableId(Resource.drawable.snailbilling_uc_password);
				int stringId = ResUtil.getStringId(Resource.string.snailbilling_user_center_text_pwd);
				data.add(new UserCenterAdapter.AdapterData(imageId, stringId));
			} else if (userCenterTypes.get(i) == ONLINE_QUESTION) {
				int imageId = ResUtil.getDrawableId(Resource.drawable.snailbilling_uc_question);
				int stringId = ResUtil.getStringId(Resource.string.snailbilling_user_center_text_question);
				data.add(new UserCenterAdapter.AdapterData(imageId, stringId));
			} else if (userCenterTypes.get(i) == CUSTOM_SERVICE) {
				int imageId = ResUtil.getDrawableId(Resource.drawable.snailbilling_uc_service);
				int stringId = ResUtil.getStringId(Resource.string.snailbilling_user_center_text_phone);
				data.add(new UserCenterAdapter.AdapterData(imageId, stringId));
			}
		}
		adapter = new UserCenterAdapter(data);
		listView.setAdapter(adapter);
		if (AccountManager.getCurrentAccount().getType().equals(Account.TYPE_RANDOM)) {
		} else if (AccountManager.getCurrentAccount().getType().equals(Account.TYPE_RANDOM_OLD)) {
		} else if (AccountManager.getCurrentAccount().getType().equals(Account.TYPE_FACEBOOK)) {
		} else if (AccountManager.getCurrentAccount().getType().equals(Account.TYPE_GOOGLE)) {
		} else {
			requestValidateInfo();
		}
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		int type = userCenterTypes.get(position);
		switch (type) {
			case BIND_ACCOUNT: {
				Bundle args = new Bundle();
				args.putInt("state", 1);
				getPageManager().forward(BindAccountPage.class, args);
			}
			break;
			case BIND_MOBILE:
				if (jsonValidateInfo != null) {
					if (jsonValidateInfo.isMobileBound()) {
						Bundle args = new Bundle();
						args.putString("text", showMobile(jsonValidateInfo.getMobile()));
						getPageManager().forward(BoundMobliePage.class, args);
					} else {
						getPageManager().forward(BindMobilePage.class);
					}
				}
			break;
			case BIND_EMAIL:
				if (jsonValidateInfo != null) {
					if (jsonValidateInfo.isEmailBound()) {
						// Bundle args = new Bundle();
						// args.putString("text", showEmail(jsonValidateInfo.getEmail()));
						// getPageManager().forward(BoundEmailPage.class, args);
					} else {
						getPageManager().forward(BindEmailPage.class);
					}
				}
			break;
			case MODIFY_PASSWORD:
				if (jsonValidateInfo != null) {
					if (jsonValidateInfo.isMobileBound() || jsonValidateInfo.isEmailBound()) {
						getPageManager().forward(ModifyPwdPage.class);
					} else {
						AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
						builder.setMessage(ResUtil.getString(Resource.string.snailbilling_user_center_no_bind));
						builder.setPositiveButton(ResUtil.getString(HttpConst.string.NET_OK), new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								getPageManager().forward(ModifyPwdPage.class);
							}
						});
						builder.show();
					}
				}
			break;
			case ONLINE_QUESTION:
				WorkDataManager.getInstance().clear();
				getPageManager().forward(WorkMainPage.class);
			break;
			case CUSTOM_SERVICE:
				getPageManager().forward(ServicePage.class);
			break;
		}
	}

	private void requestValidateInfo() {
		BillingHttpApp billingHttpApp = new BillingHttpApp(getContext(), new BillingHttpApp.RequestHttpSesssionListener() {

			@Override
			public HttpSession getLoginHttpSession() {
				return new GeneralLoginSession(AccountManager.getCurrentAccount().getAccount(), AccountManager.getCurrentAccount().getPwd());
			}

			@Override
			public HttpSession getRequestHttpSesssion() {
				validateInfoSession = new ValidateInfoSession();
				return validateInfoSession;
			}
		});
		billingHttpApp.setOnHttpResultListener(this);
		billingHttpApp.request();
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(validateInfoSession)) {
				jsonValidateInfo = new ValidateInfoSession.JsonData((String) httpSession.getResponseData());
				if (jsonValidateInfo.getCode() == 1) {
					for (int i = 0; i < userCenterTypes.size(); i++) {
						if (userCenterTypes.get(i) == BIND_MOBILE) {
							if (jsonValidateInfo.isMobileBound()) {
								data.get(i).text2 = showMobile(jsonValidateInfo.getMobile());
							} else {
								data.get(i).text2 = "未绑定";
							}
						} else if (userCenterTypes.get(i) == BIND_EMAIL) {
							if (jsonValidateInfo.isEmailBound()) {
								data.get(i).text2 = showEmail(jsonValidateInfo.getEmail());
							} else {
								data.get(i).text2 = "未绑定";
							}
						}
					}
					adapter.notifyDataSetChanged();
				} else {
					Toast.makeText(getContext(), jsonValidateInfo.getMessage(), Toast.LENGTH_SHORT).show();
				}
			}
		}
	}

	private String showMobile(String mobile) {
		if (TextUtils.isEmpty(mobile)) {
			return "";
		}
		StringBuilder builder = new StringBuilder();
		char[] chars = mobile.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			if (i < 3 || i > chars.length - 4) {
				builder.append(chars[i]);
			} else {
				builder.append("*");
			}
		}
		return builder.toString();
	}

	private String showEmail(String email) {
		if (TextUtils.isEmpty(email)) {
			return "";
		}
		String[] strs = email.split("@");
		StringBuilder builder = new StringBuilder();
		char[] chars = strs[0].toCharArray();
		for (int i = 0; i < chars.length; i++) {
			if (i < 3) {
				builder.append(chars[i]);
			} else {
				builder.append("*");
			}
		}
		builder.append("@");
		builder.append(strs[1]);
		return builder.toString();
	}

}
